package com.menggudf.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import com.menggudf.entity.Medicine;

@Repository
public class MedicineDao{

    private static final Log log = LogFactory.getLog(MedicineDao.class);

    @PersistenceContext(unitName = "medicalUnit")
    private EntityManager entityManager;

    public void persist(Medicine transientInstance) {
        try {
            entityManager.persist(transientInstance);
        } catch (RuntimeException re) {
            log.error("persist failed", re);
            throw re;
        }
    }

    public void remove(Medicine persistentInstance) {
        try {
            entityManager.remove(persistentInstance);
        } catch (RuntimeException re) {
            log.error("remove failed", re);
            throw re;
        }
    }

    public Medicine merge(Medicine detachedInstance) {
        try {
            Medicine result = entityManager.merge(detachedInstance);
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public Medicine findById(Long id) {
        try {
            Medicine instance = entityManager.find(Medicine.class, id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }

    public Medicine find(String name) {
        try {
            Query query = entityManager.createQuery("from Medicine m where m.name=?1 ").setParameter(1, name);
            List<Medicine> list = query.getResultList();
            if(list!=null && list.size()>0){
                return list.get(0);
            }else{
                return null;
            }
        } catch (Exception e) {
            log.error("find fail",e);
            throw e;
        }
    }

    public List<Medicine> getList(String name, int pageSize, int offset) {
        StringBuffer sqld = new StringBuffer("from Medicine m ");
        if(!StringUtils.isEmpty(name)){
            sqld.append(" where m.name=?1 ");
        }
        sqld.append(" order by m.createdTime desc ");
        Query query;
        if(!StringUtils.isEmpty(name)){
            query = entityManager.createQuery(sqld.toString()).setParameter(1, name);
        }else{
            query = entityManager.createQuery(sqld.toString());
        }
        query.setFirstResult(offset).setMaxResults(pageSize);
        return query.getResultList();

    }

    public long getListCount(String name) {
        StringBuffer sqld = new StringBuffer("select count(*) from Medicine m ");
        if(!StringUtils.isEmpty(name)){
            sqld.append(" where m.name=?1 ");
        }
        sqld.append(" order by m.createdTime desc ");
        Query query;
        if(!StringUtils.isEmpty(name)){
            query = entityManager.createQuery(sqld.toString()).setParameter(1, name);
        }else{
            query = entityManager.createQuery(sqld.toString());
        }
        return (long) query.getSingleResult();
    }

    public Medicine getMedicineByName(String name) {
        try {
            Query query = entityManager.createQuery("from Medicine m where m.name=?1 ").setParameter(1, name);
            List<Medicine> list = query.getResultList();
            if(list!=null && list.size()>0){
                return list.get(0);
            }else{
                return null;
            }
        } catch (Exception e) {
            log.error("getMedicineByName fail",e);
            throw e;
        }
    }
}
